/**
 * 
 */
package japavo.trainer;

import japavo.tools.vocabulary.VocabularyBook;
import japavo.tools.vocabulary.VocabularyBook.ShownVocabulary;
import japavo.tools.xml.XMLParser;

import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;


/**
 *	The controll class
 */
public class VocabularyTrainer extends Observable {
	
	/**********************************************************************************************
	 ************************************ DECLARATIONS ******************************************** 
	 *********************************************************************************************/
	/**
	 * The currentyl active vocabulary book
	 */
	private VocabularyBook activeBook;
	/**
	 * The file related the the active vocabulary book
	 */
	private File activeFile;
	/**
	 * The number of the vocabulary of the active book currently shown
	 */
	private int currentVocabulary;
	/**
	 * The currently shown vocabulary
	 */
	private ShownVocabulary shownVocabulary;
	
	/**********************************************************************************************
	 ************************************ CONSTRUCTORS ******************************************** 
	 *********************************************************************************************/
	/**
	 * 
	 */
	public VocabularyTrainer() {
	}
	
	/**********************************************************************************************
	 ************************************** FUNCTIONS ********************************************* 
	 *********************************************************************************************/
	/**
	 * Shuffles the vocabularies und returns the first vocabulary to all observer. Also creates
	 * vocabulary book with default book, if none is loaded.
	 */
	public void startTraining(Object trainingRessource) {
		getActiveBook();
		activeBook.shuffleVocabularies();
		addObserver((Observer) trainingRessource);
		currentVocabulary = 0;
		shownVocabulary = activeBook.getVocabularyToShow(0);
		returnNextVocabulary();
	}
	
	/**
	 * If no vocabulary book is loaded the default will be loaded
	 * 
	 * @return the currently loaded vocabulary book
	 */
	public VocabularyBook getActiveBook() {
		// TODO maybe delete this
		if (activeBook == null) {
			activeBook = (VocabularyBook) XMLParser.loadFromXML(getDefaultFile(), VocabularyBook.class);
			activeFile = new File(System.getProperty("DEFAULT_FILE"));
		}
		return activeBook;
	}
	
	public File getActiveFile() {
		return activeFile;
	}
	
	public void setActiveFile(File file) {
		this.activeFile = file;
	}
	
	private File getDefaultFile() {
		File defaultFile = new File(System.getProperty("DEFAULT_FILE"));
		if (!defaultFile.exists())
			try {
				defaultFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
		return defaultFile;
	}
	
	/**
	 * Returns the next vocabulary in the shuffled book
	 */
	public void getNextVocabulary() {
		currentVocabulary++;
		if (currentVocabulary >= activeBook.vocabularyCount())
			return;
		shownVocabulary = activeBook.getVocabularyToShow(currentVocabulary);
		returnNextVocabulary();
	}
	
	/**
	 * Notifies observers that currently viewed vocabulary has changed
	 */
	private void returnNextVocabulary() {
		setChanged();
		notifyObservers(shownVocabulary);
	}

	/**
	 * Indicates that the vocabulary has been played so it's stats will be updated
	 * 
	 * @param correct
	 */
	public void vocabularyPlayed(boolean correct) {
		activeBook.vocabularyPlayed(currentVocabulary, correct);
	}
	
	/**
	 * Creates a new vocabulary book
	 */
	public void createNewVocabularyBook() {
		activeBook = new VocabularyBook();
		activeFile = null;
	}
	
	/**
	 * Opens a specific vocabulary book
	 * 
	 * @param file
	 */
	public void openVocabularyBook(File file) {
		activeFile = file;
		activeBook = (VocabularyBook) XMLParser.loadFromXML(file, VocabularyBook.class);
	}

	/**
	 * Saves the active vocabulary book
	 */
	public void saveVocabularyBook() {
		XMLParser.saveToXML(activeFile, activeBook);
	}
	
	/**
	 * Closes the active vocabulary book
	 */
	public void closeVocabularyBook() {
		activeBook = null;
		activeFile = null;
	}
}
